home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_file.py < prev    next >
Text File  |  2005-11-19  |  4KB  |  152 lines

  1. import sys
  2. import os
  3. from array import array
  4.  
  5. from test.test_support import verify, TESTFN, TestFailed
  6. from UserList import UserList
  7.  
  8. # verify expected attributes exist
  9. f = file(TESTFN, 'w')
  10. softspace = f.softspace
  11. f.name     # merely shouldn't blow up
  12. f.mode     # ditto
  13. f.closed   # ditto
  14.  
  15. # verify softspace is writable
  16. f.softspace = softspace    # merely shouldn't blow up
  17.  
  18. # verify the others aren't
  19. for attr in 'name', 'mode', 'closed':
  20.     try:
  21.         setattr(f, attr, 'oops')
  22.     except TypeError:
  23.         pass
  24.     else:
  25.         raise TestFailed('expected TypeError setting file attr %r' % attr)
  26. f.close()
  27.  
  28. # verify writelines with instance sequence
  29. l = UserList(['1', '2'])
  30. f = open(TESTFN, 'wb')
  31. f.writelines(l)
  32. f.close()
  33. f = open(TESTFN, 'rb')
  34. buf = f.read()
  35. f.close()
  36. verify(buf == '12')
  37.  
  38. # verify readinto
  39. a = array('c', 'x'*10)
  40. f = open(TESTFN, 'rb')
  41. n = f.readinto(a)
  42. f.close()
  43. verify(buf == a.tostring()[:n])
  44.  
  45. # verify writelines with integers
  46. f = open(TESTFN, 'wb')
  47. try:
  48.     f.writelines([1, 2, 3])
  49. except TypeError:
  50.     pass
  51. else:
  52.     print "writelines accepted sequence of integers"
  53. f.close()
  54.  
  55. # verify writelines with integers in UserList
  56. f = open(TESTFN, 'wb')
  57. l = UserList([1,2,3])
  58. try:
  59.     f.writelines(l)
  60. except TypeError:
  61.     pass
  62. else:
  63.     print "writelines accepted sequence of integers"
  64. f.close()
  65.  
  66. # verify writelines with non-string object
  67. class NonString: pass
  68.  
  69. f = open(TESTFN, 'wb')
  70. try:
  71.     f.writelines([NonString(), NonString()])
  72. except TypeError:
  73.     pass
  74. else:
  75.     print "writelines accepted sequence of non-string objects"
  76. f.close()
  77.  
  78. # verify that we get a sensible error message for bad mode argument
  79. bad_mode = "qwerty"
  80. try:
  81.     open(TESTFN, bad_mode)
  82. except IOError, msg:
  83.     if msg[0] != 0:
  84.         s = str(msg)
  85.         if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
  86.             print "bad error message for invalid mode: %s" % s
  87.     # if msg[0] == 0, we're probably on Windows where there may be
  88.     # no obvious way to discover why open() failed.
  89. else:
  90.     print "no error for invalid mode: %s" % bad_mode
  91.  
  92. f = open(TESTFN)
  93. if f.name != TESTFN:
  94.     raise TestFailed, 'file.name should be "%s"' % TESTFN
  95. if f.isatty():
  96.     raise TestFailed, 'file.isatty() should be false'
  97.  
  98. if f.closed:
  99.     raise TestFailed, 'file.closed should be false'
  100.  
  101. try:
  102.     f.readinto("")
  103. except TypeError:
  104.     pass
  105. else:
  106.     raise TestFailed, 'file.readinto("") should raise a TypeError'
  107.  
  108. f.close()
  109. if not f.closed:
  110.     raise TestFailed, 'file.closed should be true'
  111.  
  112. # make sure that explicitly setting the buffer size doesn't cause
  113. # misbehaviour especially with repeated close() calls
  114. for s in (-1, 0, 1, 512):
  115.     try:
  116.         f = open(TESTFN, 'w', s)
  117.         f.write(str(s))
  118.         f.close()
  119.         f.close()
  120.         f = open(TESTFN, 'r', s)
  121.         d = int(f.read())
  122.         f.close()
  123.         f.close()
  124.     except IOError, msg:
  125.         raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg))
  126.     if d != s:
  127.         raise TestFailed, 'readback failure using buffer size %d'
  128.  
  129. methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
  130.            'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
  131.            'xreadlines', '__iter__']
  132. if sys.platform.startswith('atheos'):
  133.     methods.remove('truncate')
  134.  
  135. for methodname in methods:
  136.     method = getattr(f, methodname)
  137.     try:
  138.         method()
  139.     except ValueError:
  140.         pass
  141.     else:
  142.         raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
  143.  
  144. try:
  145.     f.writelines([])
  146. except ValueError:
  147.     pass
  148. else:
  149.     raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
  150.  
  151. os.unlink(TESTFN)
  152.